package com.nacos.temp1.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean("restTemplate")
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        return restTemplateBuilder
                .additionalInterceptors(httpInterceptor)
                .build();
    }

    private ClientHttpRequestInterceptor httpInterceptor = (HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) -> {
        System.out.println("Headers: " + httpRequest.getHeaders());
        System.out.println("Request Method: " + httpRequest.getMethod());
        System.out.println("Request URI: " + httpRequest.getURI());
        return clientHttpRequestExecution.execute(httpRequest, bytes);
    };
}
